/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isd.global.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import com.isd.global.rn.ParametrosRN;
import javax.ejb.EJB;
import com.isd.global.modelo.HomeImagenes;
import com.isd.global.modelo.ItemBanner;
import com.isd.global.modelo.ItemVideo;
import com.isd.global.modelo.Parametro;
import com.isd.global.rn.BannerRN;
import com.isd.global.rn.VideosRN;
import com.isd.pool.modelo.Pool;
import com.isd.promocion.modelo.Promocion;
import com.isd.promocion.rn.PromocionRN;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Claudio
 */
@ManagedBean
@ApplicationScoped
public class AplicacionBean implements Serializable {

    @EJB private ParametrosRN parametroRN;
    @EJB private BannerRN     bannerRN;
    @EJB private VideosRN     videoRN;
    @EJB private PromocionRN promocionRN;
    
    private Parametro parametro;
    private List<ItemBanner> banner;
    private List<ItemVideo> videos;
    private List<Promocion> promociones;
    private List<HomeImagenes> imagenesHome;
    private List<HomeImagenes> linkImagenesHome;

    /** Creates a new instance of AplicacionBean */
    public AplicacionBean() {
    }

    @PostConstruct
    private void init() {

        actualizarDatos();
        Logger.getLogger(getClass().getName()).log(Level.INFO, "AplicacionBean OK", "AplicacionBean OK");            
    }

    public void actualizarDatos(){
        try {

            parametro = parametroRN.getParametros();
            promociones = promocionRN.getPromocionesActivas();
            banner = bannerRN.getLista();
            videos = videoRN.getLista();
            cargarImagenesHome();

        } catch (Exception ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "actualizarDatos",ex);            
        }
    }

    public synchronized void actualizarDatosBanner(){

        banner = bannerRN.getLista();

        if(banner==null) return;

        if(banner.isEmpty()) return;

        for(ItemBanner i: banner){

            if(i.getPromocion()!=null){
                if(!i.getPromocion().isActiva()){
                    banner.remove(i);
                }
            }

            if(i.getPool()!=null){
                if(!i.getPool().isActiva()){
                    banner.remove(i);
                }
            }
        }
    }


    public String verPrincipal(){

        //return "../global/index.xhtml";
        return "home";
    }

    public String verRevistaEM(){
        
        return parametro.getLinkRevistaEM();
    }

    public String navegar(String link){

        return link;
    }
    
    public String getStringFechaServer(){

        String s ="";
        Calendar f = Calendar.getInstance();
        f.setTime(new Date());

        s = s + String.valueOf(f.get(Calendar.YEAR)) + ",";
        s = s + String.valueOf(f.get(Calendar.MONTH)) + ",";
        s = s + String.valueOf(f.get(Calendar.DAY_OF_MONTH)) + ",";

        if(f.get(Calendar.AM_PM)==Calendar.AM){
            s = s + String.valueOf(f.get(Calendar.HOUR)) + ",";
        }else{
            s = s + String.valueOf(12+f.get(Calendar.HOUR)) + ",";
        }

        s = s + String.valueOf(f.get(Calendar.MINUTE)) + ",";
        s = s + String.valueOf(f.get(Calendar.SECOND)) ;
        return s;
    }

    public String fechaFin(Pool p){

        try {
            String s = "";
            Calendar f = Calendar.getInstance();
            f.setTime(p.getDisponibleHasta());

            s = s + String.valueOf(f.get(Calendar.YEAR)) + ",";
            s = s + String.valueOf(f.get(Calendar.MONTH)) + ",";
            s = s + String.valueOf(f.get(Calendar.DAY_OF_MONTH)) + ",";

            if (f.get(Calendar.AM_PM) == Calendar.AM) {
                s = s + String.valueOf(f.get(Calendar.HOUR)) + ",";
            } else {
                s = s + String.valueOf(12 + f.get(Calendar.HOUR)) + ",";
            }

            s = s + String.valueOf(f.get(Calendar.MINUTE)) + ",";
            s = s + String.valueOf(f.get(Calendar.SECOND));
            return s;
        } catch (Exception e) {
            return "";
        }
    }
    
    public List<ItemVideo> getVideos() {
        return videos;
    }

    public void setVideos(List<ItemVideo> videos) {
        this.videos = videos;
    }
    
    public Parametro getParametro() {
        return parametro;
    }

    public void setParametro(Parametro parametro) {
        this.parametro = parametro;
    }

    public List<ItemBanner> getBanner() {

        return banner;
    }

    public void setBanner(List<ItemBanner> banner) {
        this.banner = banner;
    }

    public List<Promocion> getPromociones() {
        return promociones;
    }

    public void setPromociones(List<Promocion> promociones) {
        this.promociones = promociones;
    }

    public List<HomeImagenes> getImagenesHome() {
        return imagenesHome;
    }

    public void setImagenesHome(List<HomeImagenes> imagenesHome) {
        this.imagenesHome = imagenesHome;
    }

    private void cargarImagenesHome() {

        if (parametro==null) return;

        imagenesHome = new ArrayList<HomeImagenes>();

        for(HomeImagenes i: parametro.getImagenesHome()){

            if(i.getHabilitado().equals("S")){
                imagenesHome.add(i);
            }
        }
    }

    


}
